package my.dbUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {
	private static String user = "p1";
	private static String password = "123456";
	private static String driver = "oracle.jdbc.OracleDriver";
	private static String url = "jdbc:oracle:thin://localhost:1521/orcl";

	public static Connection getConnection() {
		Connection conn = null;

		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);

		} catch (ClassNotFoundException e) {
			System.err.println("系统找不到指定类,请检查重试!");
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println("数据库连接错误!请检查数据库服务是否已启动或检查数据库参数或联系管理员!");
			e.printStackTrace();
		}
		return conn;
	}

	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
